package action;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import bean.User;
import util.HibernateUtil;

/**
 * 本类用于测试Hibernate查询方式之： 使用Criteria接口查询 这个Criteria接口只能用来查询，
 * 
 * @author 半颗柠檬、
 * 
 */
public class Query_Criteria {
	public static void main(String[] args) {
		// Query_Criteria.testSelectAll();

		// 查询单条记录
		// Query_Criteria.selectSingle();

		// 分页查询
		Query_Criteria.testPage();
	}

	/**
	 * 查询所有记录
	 */
	private static void testSelectAll() {

		Session session = null;
		try {
			// 查询操作不需要事务
			session = HibernateUtil.getSession();

			// 获取User的Criteria查询接口
			Criteria userCriteria = session.createCriteria(User.class);

			/**
			 * 添加排序规则 按照userid升序排序，且userid必须和javabean的属性名一致，不能和数据库中的字段名一致，否则报错
			 */
			userCriteria.addOrder(Order.asc("userid"));

			/**
			 * 添加查询的条件
			 */
			userCriteria.add(Restrictions.ilike("username", "%se%")); // Restrictions.ilike和Restrictions.like
																		// 模糊查询like表示不忽略大小写
			// userCriteria.add(Restrictions.between("userid", new Integer(2),
			// new Integer(3))); //查询userid在2与3之间的记录

			List<Integer> idList = new ArrayList<Integer>();
			idList.add(1);
			idList.add(2);
			idList.add(3);
			userCriteria.add(Restrictions.in("userid", idList)); // 查询userid包含idList集合的记录

			// userCriteria。list()返回User映射的表里的所有数据，兵封装为List
			List<User> userList = userCriteria.list();

			for (User user : userList) {

				System.out.println("userid==" + user.getUserid() + "\t"
						+ "username=" + user.getUsername() + "\t"
						+ "password==" + user.getPassword());

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 查询单条记录
	 */
	private static void selectSingle() {

		Session session = null;
		Transaction tran = null;

		try {
			session = HibernateUtil.getSession();
			tran = session.beginTransaction();

			Criteria userCriteria = session.createCriteria(User.class);
			userCriteria.add(Restrictions.eq("username", "admin"));

			// Criteria.uniqueResult() 返回单条结果
			User user = (User) userCriteria.uniqueResult();

			System.out.println("userid==" + user.getUserid() + "\t"
					+ "username=" + user.getUsername() + "\t" + "password=="
					+ user.getPassword());

			tran.commit();
		} catch (Exception e) {
			tran.rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();
		}

	}

	
	/**
	 * 分页管理
	 */
	private static void testPage() {
		Session session=null;
		Transaction tran=null;
		try {
			session=HibernateUtil.getSession();
			tran=session.beginTransaction();
			
			Criteria userCriteria=session.createCriteria(User.class);
			
			userCriteria.add(Restrictions.ilike("username", "%user%"));
			
			userCriteria.setFirstResult(1);  //从第几条开始显示，0代表第一条
			userCriteria.setMaxResults(2);   //pagesize  每页显示多少条
			
			
			List<User> userList=userCriteria.list();
			
			for (User user : userList) {

				System.out.println("userid==" + user.getUserid() + "\t"
						+ "username=" + user.getUsername() + "\t"
						+ "password==" + user.getPassword());

			}
			
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			HibernateUtil.closeSession();
		}

	}



}
